package com.yanqu.road.dao.impl.activity.ghost;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostDisperseData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostDisperseDataDaoImpl extends TempDataObjectDao<GhostDisperseData> {

    public static String getGhostDisperseDataMapSql = "select * from t_c_activity_ghost_disperse where activityId=? and batchNo=?";

    public Map<Integer, List<GhostDisperseData>> getGhostDisperseDataMap(int activityId, int batchNo) {
        Map<Integer, List<GhostDisperseData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostDisperseDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, batchNo);
            rs = call.executeQuery();
            while (rs.next()) {
                GhostDisperseData ghostDisperseData = parse(rs);

                if (!map.containsKey(ghostDisperseData.getType())) {
                    map.put(ghostDisperseData.getType(), new ArrayList<>());
                }

                map.get(ghostDisperseData.getType()).add(ghostDisperseData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getGhostMaxDisperseIdSql = "select max(disperseId) as maxId from t_c_activity_ghost_disperse where activityId=?";

    public Integer getGhostMaxDisperseId(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostMaxDisperseIdSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGhostDisperseDataSql = "update t_c_activity_ghost_disperse set reward=?,count=?,weight=?,notice=? where activityId=? and batchNo=? and type=? and disperseId=?";

    @Override
    protected boolean updateData(GhostDisperseData ghostDisperseData) {
        ghostDisperseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, ghostDisperseData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getCount()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getWeight()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostDisperseData.isNotice()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getDisperseId()));
        if (getDb().executeNoneQuery(updateGhostDisperseDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGhostDisperseDataSql = "insert into t_c_activity_ghost_disperse(activityId,batchNo,type,disperseId,reward,count,weight,notice) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GhostDisperseData ghostDisperseData) {
        ghostDisperseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getDisperseId()));
        para.put(i++, new DbParameter(Types.VARCHAR, ghostDisperseData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getCount()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostDisperseData.getWeight()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostDisperseData.isNotice()));
        if (getDb().executeNoneQuery(insertGhostDisperseDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GhostDisperseData parseData(ResultSet rs) throws SQLException {
        GhostDisperseData ghostDisperseData = new GhostDisperseData();
        ghostDisperseData.setActivityId(rs.getInt("activityId"));
        ghostDisperseData.setBatchNo(rs.getInt("batchNo"));
        ghostDisperseData.setType(rs.getInt("type"));
        ghostDisperseData.setDisperseId(rs.getInt("disperseId"));
        ghostDisperseData.setReward(rs.getString("reward"));
        ghostDisperseData.setCount(rs.getInt("count"));
        ghostDisperseData.setWeight(rs.getInt("weight"));
        ghostDisperseData.setNotice(rs.getBoolean("notice"));
        return ghostDisperseData;
    }

}
